# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Bartłomiej Piotrowski <nospam@bpiotrowski.pl>
pkgname=luajit
_pkgname=luajit2
pkgver=2.1_p20210510
_pkgver=${pkgver/_p/-}
pkgrel=0
_gitrev_tests=485311ff265763c2aecce155ad697a974838857c
pkgdesc="OpenResty's branch of LuaJIT"
url="https://github.com/openresty/luajit2"
arch="all !riscv64"
license="MIT"
checkdepends="
	perl
	perl-parallel-forkmanager
	sqlite-dev
	zlib-dev
	"
provides="lua"
subpackages="$pkgname-dev $pkgname-doc"
source="https://github.com/openresty/luajit2/archive/v$_pkgver/$_pkgname-$_pkgver.tar.gz
	https://github.com/openresty/luajit2-test-suite/archive/$_gitrev_tests/luajit2-test-suite-$_gitrev_tests.tar.gz
	fix-lual_newstate-returns-null-on-ppc64le.patch
	fix-compilation-error-on-ppc.patch
	fix-tests-alpine.patch
	module-paths.patch
	"
builddir="$srcdir/$_pkgname-$_pkgver"

prepare() {
	cp -Rl ../luajit2-test-suite-$_gitrev_tests test-suite

	default_prepare

	case "$CARCH" in
		# bit.tobit test failed (got 280328, expected 277312)
		arm*) rm test-suite/test/misc/bit_op.lua;;
		# 35: declaration specifier expected near '_Alignas' at line 882
		x86) rm test-suite/test/sysdep/ffi_include_std.lua;;
		# bunch of broken tests on mips64
		mips64) rm test-suite/test/ffi/ffi_jit_conv.lua \
			   test-suite/test/ffi/ffi_convert.lua \
			   test-suite/test/sysdep/catch_cpp.lua \
			   test-suite/test/sysdep/ffi_include_std.lua \
			   test-suite/test/misc/dualnum.lua ;;
	esac
}

build() {
	# These flags are recommended by OpenResty.
	local xcflags="-DLUAJIT_ENABLE_LUA52COMPAT -DLUAJIT_NUMMODE=2"
	[ "$CARCH" = x86_64 ] && xcflags="$xcflags -msse4.2"

	make amalg PREFIX=/usr XCFLAGS="$xcflags"
}

check() {
	local prefix="$builddir/test-suite/target"
	mkdir -p "$prefix"

	make install PREFIX="$prefix"

	cd test-suite
	LUA52=1 ./run-tests "$prefix"
}

package() {
	make install DESTDIR="$pkgdir" PREFIX=/usr

	install -Dm644 COPYRIGHT \
		"$pkgdir"/usr/share/licenses/$pkgname/COPYRIGHT
}

sha512sums="
c94b2c000f923f53bab88f5f90da74b3ac813fcf153d989a03b816654a26c5d50b7e5a2a70a86a0e8784431377d48f9be1635399e030d4882a0609ed786c87fb  luajit2-2.1-20210510.tar.gz
87995ffd648ca57049dadda12fa563a8c47c7735c0a34471372eeedaf4023361cf20824118e478e4466e3063ef864aa6b5ab0bbca1a5e0a3a5ef7ff044151deb  luajit2-test-suite-485311ff265763c2aecce155ad697a974838857c.tar.gz
57a60839c4416227bb0995ad0ae98f973b6bd82155fda2e6a1c8369d662d470b733582d4aeff6181771ea496d4d8af6aa684c0b45734abc5b1fd0d91d54f20bb  fix-lual_newstate-returns-null-on-ppc64le.patch
7eaf1c9f73127c7bd321cc0d4f4d9c1db87cc9ff672936a98e2f9f247aca7bdc570fc54120e90cf9bc1dc74f48d65f340f600a2bb93fd22fe1904d957a8172ec  fix-compilation-error-on-ppc.patch
9702eb7f51d52ef97be0fb5c7369bde9ce229444ae05e5387e3d1144acc03391f225ede07a7fd3799ca749a916c1d934dbac383c36720ba2e13b1bc19d8df1a5  fix-tests-alpine.patch
9c6c410e142838c5128c70570e66beb53cc5422df5ecfb8ab0b5f362637b21ab5978c00f19bc1759165df162fd6438a2ed43e25020400011b7bb14014d87c62e  module-paths.patch
"
